/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ejbs;

import dtos.AdministradorDTO;
import entities.Administrador;
import exceptions.EntidadeExistenteException;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Yeezus
 */
@Stateless
public class AdministradorBean {

    @PersistenceContext
    private EntityManager em;

    @EJB
    private FuncionarioBean funcBean;

    public void criarAdministrador(String username, String password, String name, String email) throws EntidadeExistenteException {
        try {
            if (funcBean.existeFuncionario(username)) {
                throw new EntidadeExistenteException("Esse admin já existe");
            }
            em.persist(new Administrador(username, name, password, email));

        } catch (EntidadeExistenteException e) {
            throw e;
        } catch (Exception e) {
            throw new EJBException(e.getMessage());
        }
    }
     public boolean existeAdministrador(String nome) {
        try {
            return em.find(Administrador.class, nome) != null;
        } catch (Exception e) {
            throw new EJBException(e.getMessage());
        }
    }
     
     public AdministradorDTO getAdministrador(String username){
        Administrador a = em.find(Administrador.class, username);
        return new AdministradorDTO(a.getUsername(),a.getNome(),a.getEmail());
     }
}
